package com.my_musc_service.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.my_musc_service.common.R;
import com.my_musc_service.entity.Singer;
import com.my_musc_service.service.SingerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Singer)表控制层
 *
 * @author makejava
 * @since 2021-07-09 16:23:04
 * @description 歌手相关
 */
@RestController
@RequestMapping("singer")
public class SingerController {
    /**
     * 服务对象
     */
    @Resource
    private SingerService singerService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Singer selectOne(Integer id) {
        return this.singerService.queryById(id);
    }

    @GetMapping("selectByName")
    public R selectByName(Singer singer) {

        List<Singer> singers = this.singerService.queryAll(singer);
        if(CollectionUtils.isNotEmpty(singers)){
            return R.ok().data("posterUrl", singers.get(0).getPosterUrl());
        }else {
            return R.error().message("未查询此歌手的posterUrl");
        }
    }

    /**
     * 新增歌单
     *
     * @param singer
     * @return
     */
    @PostMapping("add")
    public R add(@RequestBody Singer singer) {
        singerService.insert(singer);
        return R.ok();
    }


}
